﻿ package 
{
	import caurina.transitions.Tweener;
	import com.fox2d.Char2D;
	import com.fox2d.events.CharEvent;
	import com.fox2d.Game2D;
	import com.fox2d.Hero2D;
	import com.fox2d.KeyboardGame;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Orlando Leite
	 */
	public class Main extends Sprite 
	{
		private var game:Game2D;
		private var char:MyHero;
		
		public function Main():void 
		{
			if ( stage ) init();
			else addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		private function init( e:Event = null ):void 
		{
			removeEventListener( Event.ADDED_TO_STAGE, init );
			// entry point
			
			game = new Game2D();
			game.map = new MyMap();
			addChild( game );
			
			game.viewWidth = 800;
			game.viewHeight = 600;
			
			game.startRender();
			var door:GlassDoor = new GlassDoor();
			door.x = 112;
			door.y = 224;
			game.addObject( door );
			
			game.controller.addKey( KeyboardGame.BTN_A, Keyboard.SPACE );
			
			char = new MyHero();
			game.addLiveObject( char );
			char.setDefaultKeyboard();
			char.setPosition( 150, 300 );
			char.speed = 3;
			char.addEventListener( CharEvent.POSITION_CHANGED, updateCamPosition );
			
			game.camera.x = game.camera.y = 30;
		}
		
		private function updateCamPosition( event:CharEvent ):void
		{
			game.camera.x = char.x;
			game.camera.y = char.y;
		}
		
	}
	
}